import {defineStore} from 'pinia';
import {Base64} from 'js-base64';

export const flowServiceStore = defineStore('flowService', {
    state: () => ({
        graph: null, // 编排器
        serviceDomain: {},
        serviceTextArea: '',
        scriptType: '',
        moreNodeInfo: {}, // 同一个组件，可以同时出现多个，将对应节点的id当作key值存入对应数据
        currentNodeId: '', // 当前选中节点的id
        currentShape: '',
        selectNode: {}, // 选中的节点
    }),

    actions: {
        setGraph(data) {
            this.graph = data;
        },
        setServiceDomain(data) {
            this.serviceDomain = data;
        },
        setServiceTextArea(data) {
            data = data.startsWith('base64:') ? Base64.decode(data.replace('base64:','')) : data
            this.serviceTextArea = data;
        },
        setServiceScriptType(data) {
            this.scriptType = data;
        },
        setMoreNodeInfo(data) {
            console.log('moreNodeInfo', this, data);
            this.moreNodeInfo = data;
        },
        setCurrentNodeId(data) {
            console.log(this, data);
            this.currentNodeId = data;
        },
        setCurrentShape(data) {
            this.currentShape = data;
        },
        setSelectNode(data) {
            this.selectNode = data;
        },
        doFlowClear() {
            if (this.graph) {
                this.graph.dispose();
            }
            this.graph = null;
            this.serviceDomain = {};
            this.moreNodeInfo = {};
            this.currentNodeId = '';
            this.currentShape = '';
            this.selectNode = {};
        },
        changeServiceTextArea(data) {
            this.setServiceTextArea(data);
        },
    },
});


// const state = {
//   graph: null, // 编排器
//   serviceDomain: {},
//   serviceTextArea: '',
//   scriptType:'',
//   moreNodeInfo: {
//     // 同一个组件，可以同时出现多个，将对应节点的id当作key值存入对应数据
//   },
//   currentNodeId: '', // 当前选中节点的id
//   currentShape: '',
//   selectNode: {}, // 选中的节点
// }
// const mutations = {
//   setGraph(state, data) {
//     state.graph = data
//   },
//   setServiceDomain(state, data) {
//     state.serviceDomain = data
//   },
//    setServiceTextArea(state, data) {
//     state.serviceTextArea = data
//   },
//   setServiceScriptType(state, data) {
//     state.scriptType = data
//   },
//   setMoreNodeInfo(state, data) {
//     console.log('moreNodeInfo',state,data);
//     state.moreNodeInfo = data
//   },
//   setCurrentNodeId(state, data) {
//     console.log(state,data);
//     state.currentNodeId = data
//   },
//   setCurrentShape(state, data) {
//     state.currentShape = data
//   },
//   setSelectNode(state, data) {
//     state.selectNode = data
//   },
//   doFlowClear(state) {
//     state.graph && state.graph.dispose()
//     state.graph = null
//     state.nlbpInfo = {}
//     state.moreNodeInfo = {}
//     state.currentNodeId = ''
//     state.currentShape = ''
//     state.selectNode = {}
//   },
// }
//
// const actions = {
//   changeServiceTextArea({ commit }, data) {
//     commit('setServiceTextArea', data)
//   }
// }
//
// export default {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// }
